{% extends 'website/backend/backend_base.html' %}
{% block option-title %}
    修改文章
{% endblock %}

{% block content %}
    <div class="col-md-12">
        <form class="form-horizontal" id="data-form">
            <div class="form-group">
                <label for="title" class="col-sm-1 control-label">标题：</label>
                <div class="col-sm-9">
                    <input type="text" id="title" name="title" value="{{ blog.title }}" class="form-control" required>
                </div>
            </div>

            <div class="form-group">
                <label for="category" class="col-sm-1 control-label">分类：</label>
                <div class="col-sm-9">
                    <select name="category" id="category" class="form-control">
                        {% for category in category_list  %}
                            <option value="{{ category.id }}" {% if category.id == category.id %}
                                selected
                            {% endif %} >{{ category.name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>

             <div class="form-group">
                <label for="summary" class="col-sm-1 control-label">摘要：</label>
                <div class="col-sm-9">
                    <textarea id="summary" name="summary" rows="5" style="width: 100%;height: 300px" class="form-control">{{ blog.summary }}</textarea>
                </div>
            </div>

            <div class="form-group">
                <label for="editor_id" class="col-sm-1 control-label">内容：</label>
                <div class="col-sm-9">
                    <textarea id="editor_id" name="content" style="width: 100%;height: 300px" class="form-control">{{ blog.content }}</textarea>
                </div>
            </div>


            <script>
                KindEditor.ready(function (K){
                    window.editor = K.create('#editor_id',{
                        'uploadJson':'{% url "image-upload" %}',
                    })
                })
            </script>
            <button class="btn btn-info" id="submit-btn">修改</button>
            <input type="reset" name="reset" style="display: none;">
        </form>
    </div>
    {% include 'utils/modal.html' %}
{% endblock %}
{% block js %}
     <script type="text/javascript">
        $('#submit-btn').click(function (e){
            editor.sync()
            e.preventDefault()
            var options = {
                type:"POST",
                dataType:'json',
                url:"{% url 'blog-update' blog.pk %}",
                success:function (data){
                    var state = data.state
                    if (state === 'success'){
                        showModal('success','修改成功')
                    }else {
                        showModal('danger','修改失败')
                    }
                    $('input[type=reset]').trigger('click')
                },
                error:function (data){
                    console.log(data)
                }
            }
            $('#data-form').ajaxSubmit(options)
        })
    </script>
{% endblock %}